package cn.edu.sgu.www.mhxysy.chain.login.impl;

import cn.edu.sgu.www.mhxysy.chain.login.UserLoginHandler;
import cn.edu.sgu.www.mhxysy.config.property.EmailProperties;
import cn.edu.sgu.www.mhxysy.config.property.SystemSettingsProperties;
import cn.edu.sgu.www.mhxysy.location.entity.Content;
import cn.edu.sgu.www.mhxysy.location.entity.Location;
import cn.edu.sgu.www.mhxysy.location.util.LocationUtils;
import cn.edu.sgu.www.mhxysy.util.EmailUtils;
import cn.edu.sgu.www.mhxysy.util.IpUtils;
import cn.edu.sgu.www.mhxysy.util.TimerUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.TimerTask;

/**
 * 登录处理-账号登录邮箱提醒
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Slf4j
@Component
public class EmailSendHandler implements UserLoginHandler {

    private Object params;
    private UserLoginHandler next;

    private final EmailUtils emailUtils;
    private final EmailProperties emailProperties;
    private final SystemSettingsProperties systemSettingsProperties;

    @Autowired
    public EmailSendHandler(
            EmailUtils emailUtils,
            EmailProperties emailProperties,
            SystemSettingsProperties systemSettingsProperties) {
        this.emailUtils = emailUtils;
        this.emailProperties = emailProperties;
        this.systemSettingsProperties = systemSettingsProperties;
    }

    @Override
    public void handle() {
        if (emailProperties.isEnable()) {
            // 得到客户端IP地址
            String clientIp = IpUtils.getIp();

            TimerUtils.schedule(new TimerTask() {
                @Override
                public void run() {
                    try {
                        String address = "广东广州";

                        // 使用真实定位
                        if (systemSettingsProperties.isUseRealLocation()) {
                            // 使用百度地图API通过IP地址获取定位
                            Location location = LocationUtils.getLocation(clientIp);

                            // 得到定位地址
                            Content content = location.getContent();

                            if (content != null) {
                                address = content.getAddress();
                            } else {
                                log.debug("根据ip[{}]获取定位失败：{}", clientIp, JSON.toJSONString(location));
                            }
                        }

                        String username = (String) params;
                        String zoneId = systemSettingsProperties.getZoneId();
                        // 定义日期格式
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

                        String text = "您的账号" + username + "在" + address + "登录了。["
                                + LocalDateTime.now(ZoneId.of(zoneId)).format(formatter) + "]";

                        emailUtils.sendMessage(text);

                        log.debug("成功发送账号登录提醒邮件...");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }, 3000);
        }

        if (next != null) {
            next.handle();
        }
    }

    @Override
    public void setParams(Object params) {
        this.params = params;
    }

    @Override
    public void setNext(UserLoginHandler next) {
        this.next = next;
    }

}